/**
 * 
 */
package fhq.hcmute.modernshopping.preference;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;
import fhq.hcmute.modernshopping.R;

/**
 * @author: Hien Nguyen Le
 * @emai: hien.nguyenle.it@gmail.com
 * 
 */
public class SettingsPreference extends PreferenceFragment implements
		OnSharedPreferenceChangeListener {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
		updateListLanguages();
		updateLimitCost();
	}

	private void updateListLanguages() {
		ListPreference preference = (ListPreference) findPreference("PREF_LIST_LANGUAGES");
		CharSequence entry = ((ListPreference) preference).getEntry();
		preference.setSummary(getString(R.string.preference_summary_language)
				+ " " + entry);
	}

	private void updateLimitCost() {
		EditTextPreference preference = (EditTextPreference) findPreference("PREF_LIMIT_COST");
		CharSequence entry = ((EditTextPreference) preference).getText();
		preference.setSummary(getString(R.string.preference_title_limit_cost)
				+ " " + entry);
	}

	@Override
	public void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals("PREF_LIST_LANGUAGES")) {
			updateListLanguages();
		} else if (key.equals("PREF_LIMIT_COST")) {
			updateLimitCost();
		}
	}
}
